Baklava

What is a playfield?

Baklava's sprites (graphical objects) can only appear inside a Baklava "playfield." To provide an environment for your sprites, you must create a baklava.Playfield object and add it to your Java applet. Then you can create as many sprite objects as you like, within the playfield.

Other responsibilities of playfields

In addition to providing an environment for sprites, playfields are also responsible for delivering global timer events to your applet, if you choose to implement the GlobalTimerObsever interface.

Also, playfields have convenience methods to perform tasks such as fetching images from the web server.

Example: An Applet with a Playfield

A very simple applet containing a playfield might look like this. Note that this applet does not use a layout manager and gives the entire applet area to the playfield. It is also possible to create playfields that share the applet with other controls by using a layout manager such as java.awt.GridBagLayout. This playfield contains only one sprite for simplicity.
// Be sure to import the baklava package
import baklava.*;

// A typical applet class declaration
public class MyApplet extends Applet {
	// A reference to the playfield
	Playfield p;

	// The URL from which we will fetch images
	// CHANGE THIS, of course!
	String base = "http://www.mysite.com/graphics/";

	// The web browser calls init when it is ready for the
	// applet to start operating	
	public void init() {
		// Use no layout manager
		setLayout(null);

		// Pass a reference to the applet, as well as
		// the width and height of the applet
		p = new Playfield(this, bounds().width, bounds().height); 

		// Create a simple sprite
		Sprite s = new Sprite(p);

		// Tell the sprite to bounce at the edges of the playfield
		s.setEdgeHandling(Sprite.edgeBounce);

		// Fetch an image
		Image image = p.getImage(base + "wanderer.gif");

		// Set this as the image for the sprite
		s.setImage(image);

		// Set a direction (45 degrees)
		s.setDirection(45);

		// Start it moving: 30 pixels per second
		s.setSpeed(30);

		// Now start movement in the playfield
		p.start();
	
		// Now display the playfield
		add(p);
	}	
	// When the user leaves the web page
	public void stop() {
		p.suspend();
	}
	// When the user comes back
	public void start() {
		p.resume();
	}
	// When the browser wants the applet to go away
	public void destroy() {
		p.stop();
	}
};

baklava.Playfield Method Reference

This section provides a reference guide to the publicly accessible methods (functions) available in the baklava.Playfield class.
Playfield(Applet appletArg, int wArg, int hArg)
This method is the constructor of the class, used to create a new playfield. Note that the applet must be the first argument to this method (see the example below). The width and height must also be set at construction time. For example:
import baklava.*;

class MyApplet extends Applet {
	public void init() {
		setLayout(null);
		Playfield p = 
			new Playfield(this, 
				bounds().width, bounds().height);

		// Create various sprites...

		// Now start movement in the playfield
		p.start();
	}
};
void setGlobalTimerObserver(GlobalTimerObserver observerArg)
This method is used by applets that wish to receive global timer events. The applet must implement the baklava.GlobalTimerObserver interface, call this method, implement a public void globalTimer(int timerId) method at which to receive the events, and then call setGlobalTimer as desired to set timers. Example:
import baklava.*;

// Be sure to specify that we implement the GlobalTimerObserver interface
class MyApplet extends Applet implements GlobalTimerObserver {
	static final int helloTimer = 1;
	Playfield p;
	public void init() {
		setLayout(null);
		p = new Playfield(this, 
				bounds().width, bounds().height);
		// This is valid because we implement GlobalTimerObserver
		p.setGlobalTimerObserver(this);
		// Send a helloTimer event in 1 second (1000 milliseconds)
		setGlobalTimer(1000, helloTimer);

		// Create various sprites...

		// Now start movement in the playfield
		p.start();
	}
	public void globalTimer(int timerId)
	{
		// This will print 1 in the java console.
		system.out.println(timerId);
		// Now set the timer again.
		setGlobalTimer(1000, helloTimer);
	}
};
void setGlobalTimer(int delay, int timerId)
This method is used to set a global timer. A global timer event will be delivered after delay milliseconds (thousands of a second). The timerId argument will be passed to the globalTimer method of the global timer observer. For more information, and an example, see the discussion of the setGlobalTimerObserver method. See also Sprite.setTimer for a way to send a timer event to an individual sprite.
void suspend()
This method is used to temporarily suspend all activity in the playfield until the resume method is called. The playfield will continue to repaint itself as needed. Technical note: To avoid problems with some implementations of java, the Thread.suspend() function is not used in the implementation. Instead, baklava spends most of its time "sleeping" to conserve CPU, and does not simulate motion while suspended. You may safely call Playfield.suspend() in all implementations of java.
void resume()
This method is used to resume activity in the playfield after the method has been called.
void start()
This method is used to start activity in the playfield. Until this method is called, sprites do not appear in the playfield and their movement is not calculated. It is very important to call this method after initializing the playfield and the sprites. For a way to suspend and restart the playfield's activity later, see the suspend and resume methods.
Image getImage(String url) OR Image getImage(URL url)
The getImage method accesses the indicated URL and downloads an image from that location. This image can then be used as an argument to the Sprite.setImage method or the Sprite.setTile method. Images can be in any format supported by the Java implementation, typically GIF or JPEG. If the start method has not yet been called for the playfield, Baklava will display a progress indicator in the playfield until all of the requested images have been downloaded successfully. You can change this behavior using the setProgressMessage and setShowProgress methods. For best results, first call getImage for all of the images you will need. Then call Sprite.setImage to take advantage of each of them. Note that transparent GIFs are fully supported by Java, and transparent pixels are ignored by Baklava when determining collisions between sprites. This is allows non-square objects to behave in realistic ways.
void progressStart()
Baklava features a progress indicator display which is automatically used to indicate that images are being downloaded. In addition, you can use this progress indicator for other purposes by calling progressStart to indicate the beginning of a lengthy operation and progressEnd to indicate the end of the operation. Calls to these methods can be freely nested, as long as there is a progressEnd call for each corresponding progressStart call. See also setProgressMessage.
void progressEnd()
The progressEnd method is used to shut off the progress indicator. See progressStart for more information.
void imagesNeededNow
In certain situations, the getImage method may yield surprising results. Specifically, if you call getImage to fetch an Image object and then immediately call getWidth() on that Image object, you will probably get a width of -1, which indicates that the width is not yet known. This is because Baklava attempts to delay image loading until several images can be downloaded at once for best performance, or until the images are needed by a Sprite.setImage or Sprite.setTile call. In situations in which you need the image data before calling one of these methods, you can call imagesNeededNow to force the playfield to immediately download all pending images.
int getWidth()
The getWidth method returns the width of the playfield, in pixels.
int getHeight()
The getHeight method returns the height of the playfield, in pixels.
void stop()
The stop method immediately stops all activity in the playfield and discards all sprites by calling the goodbyeAll method. For a way to temporarily pause activity in the playfield, consider the suspend method.
void goodbyeAll(Class whichClass) OR void goodbyeAll()
The goodbyeAll method is used to dismiss all of the sprites in the playfield, or to dismiss all sprites of a particular class. The latter form of the method is very useful if you have derived several subclasses from the sprite class. Example:
	// Dismiss only sprites of the Scone subclass.
	void dismissScones(Playfield p) {
		p.goodbyeAll(Class.forName("Scone"));
	}
void clearGlobalTimers()
This method is used to clear all currently pending global timers, if any. Global timers are set using the setGlobalTimer method.
void setTimerAll(int delay, int timerId)
This method is used to set a timer for every sprite in the playfield. This is different from a global timer in that a separate timer event is delivered to each and every sprite's Sprite.timer method, rather than to the global timer observer or a single sprite. The timer method of each sprite will be invoked with the specified timer id after the specified number of milliseconds (thousandths of a second). See also the next entry for a way to set a timer for all sprites of a specific subclass.
void setTimerAll(Class c, int delay, int timerId)
This method is used to set a timer for every sprite of a particular subclass. A separate timer event is delivered to the Sprite.timer method of each sprite of that class. The timer method of each sprite will be invoked with the specified timer id after the specified number of milliseconds (thousandths of a second). Example:
	// Tell all of the scones to move left one second from now.
	void dismissScones(Playfield p) {
		p.setTimerAll(Class.forName("Scone"), 1000, Scone.timerLeft);
	}
void setShowProgress(boolean showProgressArg)
Normally, Baklava only displays a progress indicator when loading images before the start method has been called. To request this behavior at all times, call the setShowProgress method with an argument of true. To restore the default behavior, invoke setShowProgress with an argument of false.
void setProgressMessage(String progressMessageArg)
By default, the progress indicator displayed by Java includes the words Loading Images. You can change this text by calling the setProgressMessage method with your preferred text.
int angleOfVector(int x1, int y1, int x2, int y2)
This method is used to calculate the angle, in degrees, of a vector extending from the point (x1,y1) to the point (x2,y2). It is often more convenient to use the Sprite.setDirectionToward method, which can be used to direct a particular sprite toward any other sprite or point in the playfield.

Up to Index
Copyright 1996 by Boutell.Com, Inc.